package advertising

import (
	"context"
	"device-admin/internal/app/grpc/client"
	"device-admin/internal/app/grpc/protoc/advertisingc"
	"fmt"

	"192.168.1.75/go-pkg/errorx"
)

type SupportActivity struct{}

// 创建支持活动
func (SupportActivity) SupportActivityCreate(ctx context.Context, params *advertisingc.SupportActivityCreateReq) (id string, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.SupportActivityCreate(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		if rsp != nil {
			return "", errorx.New(rsp.Msg, int(rsp.Code))
		}
		return "", errorx.New(fmt.Sprintf("创建支持活动错误: %s", rspErr.Error()), -1)
	}
	return rsp.Id, nil
}

// 更新支持活动
func (SupportActivity) SupportActivityUpdate(ctx context.Context, params *advertisingc.SupportActivityUpdateReq) (err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.SupportActivityUpdate(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		if rsp != nil {
			return errorx.New(rsp.Msg, int(rsp.Code))
		}
		return errorx.New(fmt.Sprintf("更新支持活动错误: %s", rspErr.Error()), -1)
	}
	return nil
}

// 删除支持活动
func (SupportActivity) SupportActivityDelete(ctx context.Context, params *advertisingc.SupportActivityDeleteReq) (err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.SupportActivityDelete(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		if rsp != nil {
			return errorx.New(rsp.Msg, int(rsp.Code))
		}
		return errorx.New(fmt.Sprintf("删除支持活动错误: %s", rspErr.Error()), -1)
	}
	return nil
}

// 获取活动详情
func (SupportActivity) SupportActivityDetail(ctx context.Context, params *advertisingc.SupportActivityDetailReq) (
	data *advertisingc.SupportActivityDetailRsp_SupportActivityDetail, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.SupportActivityDetail(ctx, params)
	if rspErr != nil {
		if rsp != nil {
			return nil, errorx.New(fmt.Sprintf("获取支持活动详情错误: %s, %s", rspErr.Error(), rsp.Msg), int(rsp.Code))
		}
		return nil, errorx.New(fmt.Sprintf("获取支持活动详情错误: %s", rspErr.Error()), -1)
	}
	if rsp == nil {
		return nil, errorx.New("获取支持活动详情返回空", -1)
	}
	if rsp.Code != 0 {
		return nil, errorx.New(rsp.Msg, int(rsp.Code))
	}
	return rsp.Data, nil
}

// 获取支持活动列表
func (SupportActivity) SupportActivityList(ctx context.Context, params *advertisingc.SupportActivityListReq) (
	data []*advertisingc.SupportActivityListRsp_SupportActivityListData_SupportActivityItem, count int64, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.SupportActivityList(ctx, params)
	if rspErr != nil {
		if rsp != nil {
			return data, count, errorx.New(fmt.Sprintf("获取支持活动列表错误: %s, %s", rspErr.Error(), rsp.Msg), int(rsp.Code))
		}
		return data, count, errorx.New(fmt.Sprintf("获取支持活动列表错误: %s", rspErr.Error()), -1)
	}
	if rsp == nil {
		return data, count, errorx.New("获取支持活动列表返回空", -1)
	}
	if rsp.Code != 0 {
		return data, count, errorx.New(rsp.Msg, int(rsp.Code))
	}
	if rsp.Data == nil {
		return data, count, nil
	}
	if rsp.Data.List == nil {
		rsp.Data.List = []*advertisingc.SupportActivityListRsp_SupportActivityListData_SupportActivityItem{}
	}
	return rsp.Data.List, rsp.Data.Count, nil
}

// 修改活动状态
func (SupportActivity) SupportActivityChangeStatus(ctx context.Context, params *advertisingc.SupportActivityChangeStatusReq) (err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.SupportActivityChangeStatus(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		if rsp != nil {
			return errorx.New(rsp.Msg, int(rsp.Code))
		}
		return errorx.New(fmt.Sprintf("修改活动状态错误: %s", rspErr.Error()), -1)
	}
	return nil
}

// 检查同名活动是否存在
func (SupportActivity) SupportActivitySameNameExist(ctx context.Context, params *advertisingc.SupportActivitySameNameExistReq) (count int64, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.SupportActivitySameNameExist(ctx, params)
	if rspErr != nil {
		if rsp != nil {
			return 0, errorx.New(fmt.Sprintf("检查同名活动错误: %s, %s", rspErr.Error(), rsp.Msg), int(rsp.Code))
		}
		return 0, errorx.New(fmt.Sprintf("检查同名活动错误: %s", rspErr.Error()), -1)
	}
	if rsp == nil {
		return 0, errorx.New("检查同名活动返回空", -1)
	}
	if rsp.Code != 0 {
		return 0, errorx.New(rsp.Msg, int(rsp.Code))
	}
	return rsp.Count, nil
}

// 根据素材ID获取关联的活动列表
func (SupportActivity) SupportActivityGetActivityListByMaterialIDs(ctx context.Context, params *advertisingc.SupportActivityGetActivityListByMaterialIDsReq) (
	data []*advertisingc.SupportActivityGetActivityListByMaterialIDsRsp_SupportActivityItem, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.SupportActivityGetActivityListByMaterialIDs(ctx, params)
	if rspErr != nil {
		if rsp != nil {
			return nil, errorx.New(fmt.Sprintf("根据素材ID获取关联的活动列表错误: %s, %s", rspErr.Error(), rsp.Msg), int(rsp.Code))
		}
		return nil, errorx.New(fmt.Sprintf("根据素材ID获取关联的活动列表错误: %s", rspErr.Error()), -1)
	}
	if rsp == nil {
		return nil, errorx.New("根据素材ID获取关联的活动列表返回空", -1)
	}
	if rsp.Code != 0 {
		return nil, errorx.New(rsp.Msg, int(rsp.Code))
	}
	if rsp.List == nil {
		rsp.List = []*advertisingc.SupportActivityGetActivityListByMaterialIDsRsp_SupportActivityItem{}
	}
	return rsp.List, nil
}

// 获取活动关联的素材ID列表
func (SupportActivity) SupportActivityGetMaterialIDsList(ctx context.Context, params *advertisingc.SupportActivityGetMaterialIDsListReq) (
	data []*advertisingc.SupportActivityGetMaterialIDsListRsp_SupportActivityMaterialIDsItem, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.SupportActivityGetMaterialIDsList(ctx, params)
	if rspErr != nil {
		if rsp != nil {
			return nil, errorx.New(fmt.Sprintf("获取活动关联的素材ID列表错误: %s, %s", rspErr.Error(), rsp.Msg), int(rsp.Code))
		}
		return nil, errorx.New(fmt.Sprintf("获取活动关联的素材ID列表错误: %s", rspErr.Error()), -1)
	}
	if rsp == nil {
		return nil, errorx.New("获取活动关联的素材ID列表返回空", -1)
	}
	if rsp.Code != 0 {
		return nil, errorx.New(rsp.Msg, int(rsp.Code))
	}
	if rsp.List == nil {
		rsp.List = []*advertisingc.SupportActivityGetMaterialIDsListRsp_SupportActivityMaterialIDsItem{}
	}
	return rsp.List, nil
}

// 更新活动素材ID
func (SupportActivity) SupportActivitySetMaterialIDs(ctx context.Context, params *advertisingc.SupportActivitySetMaterialIDsReq) (err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.SupportActivitySetMaterialIDs(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		if rsp != nil {
			return errorx.New(rsp.Msg, int(rsp.Code))
		}
		return errorx.New(fmt.Sprintf("更新活动素材ID错误: %s", rspErr.Error()), -1)
	}
	return nil
}

